package com.chen.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


// configuring a servlet using web.xml
public class HelloServlet extends HttpServlet {

    // method2:  servletConfig cached from init parameter
    private ServletConfig servletConfig;
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config); // key point : make sure the GenericServlet's config is set
        servletConfig=config;
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //  method 1 : obtain the servletConfig object
        ServletConfig servletConfig = this.getServletConfig();
        String charset = servletConfig.getInitParameter("charset");
        System.out.println("the obtained via servletConfig:"+charset);








        ServletContext servletContext = getServletContext();
        String value = servletContext.getInitParameter("key1");
        System.out.println("the obtained via servletContext:"+value);



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req,resp);
    }
}
